package com.itheima.info.manger.system.controller;

import com.itheima.info.manger.system.domain.Student;
import com.itheima.info.manger.system.service.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService service = new StudentService();

    //成员方法 展示功能
    //参数 无参数
    //返回值 无返回值
    public final void start() {
        loop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出学生系统");

            String choice = sc.next();
            switch (choice) {
                case "1":
                    // System.out.println("新增学生");
                    addStudent();

                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    // System.out.println("查看学生");
                    findAllStudents();
                    break;
                case "5":
                    //break loop;
                    return;

                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    public  final void addStudent() {
        String sid = null;
        while (true) {
            System.out.println("输入学号");
            sid = sc.next();
            boolean exist = service.isExist(sid);
            if(exist){
                System.out.println("已经存在,请重新输入");
            }else{
                break;
            }
        }
        Student student = getStudentInfo(sid);
        boolean b = service.addStudent(student);
        if(b){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
    //录入学生信息
    public abstract Student getStudentInfo(String sid);


    public final void deleteStudent() {
        String sid = getSid();
        service.deleteStudent(sid);
        System.out.println("删除成功");
    }
    // 重复录入学号 知道录存在为止
    public final String getSid() {
        String sid = null;
        while (true) {
            System.out.println("输入学号");
            sid = sc.next();
            boolean exist = service.isExist(sid);
            if(exist){
                //删除
                break;
            }else{
                System.out.println("不存在请重新输入");
            }
        }
        return sid;
    }

    public  final void updateStudent() {
        String sid = getSid();
        Student newStu = getStudentInfo(sid);
        service.updateStudent(sid,newStu);
        System.out.println("修改成功");
    }

    public  final void findAllStudents() {
        Student[] students = service.findAllStudents();
        if(students==null){
            System.out.println("没有数据,请先添加");
            return;
        }
        //不为null 有数据 展示数据
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < students.length; i++) {
            //students[i]
            Student student = students[i];
            if (student != null) {
                System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "岁\t" + student.getBirthday());
            }
        }
    }
}
